﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using System.Data.Entity; // needed for lambda includes



namespace TheCup.ApiControllers
{

    public class RoundController : ApiController
    {
        TheCupContext ctx = new TheCupContext();

        ApiControllers.PlayerController pc = new ApiControllers.PlayerController();
            
        public IEnumerable<Round> Get()
        {
            return (from i in ctx.Rounds.Include(ed => ed.Editor).Include(c => c.Course)
                    select i).OrderByDescending(d => d.DateEntry).AsEnumerable();
        }

        public IEnumerable<Round> GetForPlayer(int id)
        {
            return (from i in ctx.Rounds.Include(ed => ed.Editor).Include(c => c.Course).Include(ev => ev.Event).Include(sc => sc.Scorecards)
                    where i.Editor.Id.Equals(id)
                    select i).OrderByDescending(d => d.DateEntry).ToList();
        }

        public IEnumerable<Player> GetUnlinkedPlayers(int id)
        {
            List<Player> players = new List<Player>();
            players = pc.Get().ToList();

            Round round = this.Get(id);
            round = (from i in ctx.Rounds.Include(sc => sc.Scorecards)
                     where i.Id.Equals(id)
                     select i).FirstOrDefault();
            players.RemoveAll(x => round.Scorecards.Where(sc => sc.PlayerId == x.Id).Count() > 0);

            return players;
        }

        public IEnumerable<Round> Take(int take)
        {
            return (from i in ctx.Rounds.Include(ed => ed.Editor).Include(c => c.Course)
                    select i).OrderByDescending(d => d.DateEntry).Take(take).ToList();
        }

        public Round Get(int id)
        {
            Round e = ctx.Rounds
                .Include(p => p.Editor)
                .Include(course => course.Course)
                .Include(ev => ev.Event)
                .Include(s => s.Scorecards)
                .Include(b => b.Scorecards.Select(p => p.Tee))
                .Include(b => b.Scorecards.Select(p => p.Player))
                .First(p => p.Id == id);
            
            if (e == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }
            
            return e;
        }

        // POST api/<controller>
        [Authorize]
        public void Post([FromBody]Round e)
        {
            e.uuid = Guid.NewGuid();
            e.Created = DateTime.UtcNow;
            e.ApplyRules();
            ctx.Rounds.Add(e);
            ctx.SaveChanges();
        }

        // PUT api/<controller>/5
        [Authorize]
        public void Put(int id, [FromBody]Round e)
        {
            if (ModelState.IsValid)
            {
                Round r = this.Get(id);
                r.DateEntry = e.DateEntry;
                r.IsPrivate = e.IsPrivate;
                r.EventId = e.EventId;
                r.ApplyRules();
                ctx.SaveChanges();
            }
        }

        // DELETE api/<controller>/5
        [Authorize]
        public HttpResponseMessage Delete(int id)
        {
            Round r = this.Get(id);
            if (r == null)
            {
                return new HttpResponseMessage(HttpStatusCode.NotFound);
            }
            else if (r.Scorecards.Count > 0)
            {
                return new HttpResponseMessage(HttpStatusCode.Forbidden);
            }
            else
            {
                if (r.PlayerId == pc.GetLoggedInPlayer().Id)
                {
                    ctx.Rounds.Remove(r);
                    ctx.SaveChanges();
                    return new HttpResponseMessage(HttpStatusCode.OK);
                }
                else
                {
                    return new HttpResponseMessage(HttpStatusCode.Forbidden);
                }
            }           
        }
    }
}